package logic

import (
	"context"
	"shopping-mall/shopping_rpc/shoppingrpcclient"

	"shopping-mall/api/internal/svc"
	"shopping-mall/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateProductLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateProductLogic {
	return &UpdateProductLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateProductLogic) UpdateProduct(req *types.UpdateProductRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	product, err := l.svcCtx.ShopRpc.UpdateProduct(l.ctx, &shoppingrpcclient.UpdateProductRequest{
		UserId:    req.UserID,
		ProductId: req.ProductID,
		Title:     req.Title,
		Price:     req.Price,
		Image:     req.Image,
		ClassId:   req.ClassID,
	})
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: err.Error(),
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Code:    200,
		Message: "商品修改成功",
		Data:    product,
	}, nil
}
